package com.mo.lib.utils

import android.os.Build
import android.util.Log
import androidx.annotation.StringRes
import com.dylanc.longan.appVersionName
import com.hjq.toast.ToastUtils
import com.mo.lib.constans.AOP_TAG_GPS_DIALOG
import com.mo.lib.constans.AOP_TAG_NET
import com.xuexiang.xaop.annotation.Intercept
import java.util.*

/**
 * @ author：mo
 * @ data：2022/3/25:17:03
 * @ 功能：
 */
/** 吐司*/
fun showToast(toast: CharSequence) {
    ToastUtils.show(toast)
}

fun showToast(@StringRes id: Int) {
    ToastUtils.show(id)
}

fun showToast(objects: Objects) {
    ToastUtils.show(objects)
}
object KLog {
    private const val V = 0x1
    private const val D = 0x2
    private const val I = 0x3
    private const val W = 0x4
    private const val E = 0x5
    private const val A = 0x6
    private const val MAX_LENGTH = 4000
    private const val customTagPrefix = "mo_x_log"

    /**
     * 分隔器
     */
    private const val SEPARATOR = "\t\t\t"

    /**
     * 骚话
     */
    var TAG = "==矮有钱垫脚，丑有钱整好，只有穷才是一辈子的烦恼！=="

    /**
     * log的信息有4K的限制，这里判断，分割一下
     */
    private fun printDefault(type: Int, msg: String) {
        var index = 0
        val length = msg.length
        val countOfSub = length / MAX_LENGTH
        if (countOfSub > 0) {
            for (i in 0 until countOfSub) {
                val sub = msg.substring(index, index + MAX_LENGTH)
                printSub(type, TAG + sub)
                index += MAX_LENGTH
            }
            printSub(type, TAG + msg.substring(index, length))
        } else {
            printSub(type, TAG + msg)
        }
    }

    private fun printSub(type: Int, sub: String) {
        Log.i(customTagPrefix, "╔═════════════════════════════飞哥告诉你，这是log！^_^ \"══════════════════════════════════════════════════════════")
        Log.i(customTagPrefix, "                        ")
        when (type) {
            V -> Log.v(customTagPrefix, sub)
            D -> Log.d(customTagPrefix, sub)
            I -> Log.i(customTagPrefix, sub)
            W -> Log.w(customTagPrefix, sub)
            E -> Log.e(customTagPrefix, sub)
            A -> Log.wtf(customTagPrefix, sub)
            else -> {
            }
        }
        Log.i(customTagPrefix, "                                    ")
        Log.i(customTagPrefix, "╚═══════════════════════════════════════════════════════════════════════════════════════")
    }

    fun v(message: String) {
        printDefault(V, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    fun d(message: String) {
        printDefault(D, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    fun i(message: String) {
        printDefault(I, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    fun w(message: String) {
        printDefault(W, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    fun e(message: String) {
        printDefault(E, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    fun a(message: String) {
        printDefault(A, getLogInfo(Thread.currentThread().stackTrace[3]) + message)
    }

    /**
     * 输出日志所包含的信息
     */
    private fun getLogInfo(stackTraceElement: StackTraceElement): String {
        val logInfoStringBuilder = StringBuilder()
        logInfoStringBuilder.append("                 \n")
        logInfoStringBuilder.append("手机型号：" + Build.BRAND).append(SEPARATOR)
        logInfoStringBuilder.append("版  本 码：$appVersionName" ).append(SEPARATOR)
        logInfoStringBuilder.append("线  程 id：" + Thread.currentThread().id).append(SEPARATOR)
        logInfoStringBuilder.append("线  程 名：" + Thread.currentThread().name).append(SEPARATOR)
        logInfoStringBuilder.append("文  件 名：" + stackTraceElement.fileName).append(SEPARATOR)
        logInfoStringBuilder.append("类       名：" + stackTraceElement.className).append(SEPARATOR)
        logInfoStringBuilder.append("""
    
    方  法 名：${stackTraceElement.methodName}
    """.trimIndent()).append(SEPARATOR)
        logInfoStringBuilder.append("行       号：" + stackTraceElement.lineNumber)
        logInfoStringBuilder.append("\n内       容：")
        return logInfoStringBuilder.toString()
    }
}